﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using BlueMarineWebClient.BlueMarineService;
using BlueMarineWebClient.DO;

namespace BlueMarineWebClient.user
{
    public partial class booking : Page
    {
        private ExternalServiceClient _exService;

        public booking()
        {
            _exService = new ExternalServiceClient("WSHttpBinding_IExternalService")
                             {
                                 ClientCredentials =
                                     {
                                         UserName =
                                             {
                                                 Password = ConfigurationManager.AppSettings[1],
                                                 UserName = ConfigurationManager.AppSettings[0]
                                             },
                                         ServiceCertificate =
                                             {
                                                 Authentication =
                                                     {CertificateValidationMode = X509CertificateValidationMode.None}
                                             }
                                     }
                             };
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var list = (List<bookInfo>) Session["listBooking"];
            if (list == null)
            {
                string cusUserName = "Guest, Please Log In fo Booking !!";
                if (Membership.GetUser() != null)
                {
                    var membershipUser = Membership.GetUser();
                    if (membershipUser != null) cusUserName = membershipUser.UserName;
                }

                lblBookTitle.Text = "Hi " + cusUserName + "<BR /> No Room Selected";
                btnAction.Text = "Check Room";
            }
            else
            {
                string cusUserName = "Guest, Please Log In fo Booking !!";
                if (Membership.GetUser() != null)
                {
                    var membershipUser = Membership.GetUser();
                    if (membershipUser != null) cusUserName = membershipUser.UserName;
                }

                lblBookTitle.Text = "Hi " + cusUserName + " !";
                BindGrid();

                btnAction.Text = "Book Now";
            }
        }

        private bool doBooking()
        {
            try
            {
                var list = (List<bookInfo>) Session["listBooking"];
                if (list == null)
                {
                    string cusUserName = "Guest, Please Log In fo Booking !!";
                    if (Membership.GetUser() != null)
                    {
                        var membershipUser = Membership.GetUser();
                        if (membershipUser != null) cusUserName = membershipUser.UserName;
                    }

                    lblBookTitle.Text = "Hi " + cusUserName + "<BR /> No Room Selected";
                    btnAction.Text = "Check Room";
                }
                else
                {
                    var membershipUser = Membership.GetUser();
                    if (membershipUser != null)
                    {
                        string cusUserName = membershipUser.UserName;
                        int cusID = _exService.CustomerGetByAccount(cusUserName).CustomerID;
                        var listRoomID = new List<int>();
                        var inDate = new DateTime();
                        var outDate = new DateTime();
                        foreach (bookInfo bookinfo in list)
                        {
                            inDate = bookinfo.InDate;
                            outDate = bookinfo.OutDate;

                            listRoomID.Add(bookinfo.RoomID);
                        }

                        _exService.BookingAdd(cusID, listRoomID, inDate, outDate, ConfigurationManager.AppSettings[0],
                                              ConfigurationManager.AppSettings[1]);
                    }
                }


                return true;
            }
            catch (Exception ex)
            {
                string mess = ex.Message;
                lblError.Text = mess;
                return false;
            }
        }

        private void BindGrid()
        {
            bookingcart cart = bookingcart.GetCart(Session);
            gvBookingList.DataSource = cart.Items;
            gvBookingList.DataBind();
        }

        protected void gvBookingList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[4].Text = bookingcart.GetCart(Session).TotalAmount.ToString("c");
            }
        }

        protected void gvBookingList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bookingcart.GetCart(Session).Remove(e.RowIndex);
            BindGrid();
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            if (btnAction.Text == "Check Room")
            {
                Response.Redirect("/Booking.aspx");
            }
            else
            {
                bool result = doBooking();
                if (result)
                {
                    lblBookTitle.Text = "Booking Successful";
                    lblError.Text =
                        "<a href='/'>Back to HomePage</a><br />We will contact You for confirming within 24 hours";
                    btnAction.Visible = false;
                    btnAction.Text = "Check Room";
                }
                else
                    lblBookTitle.Text = "Booking FAIL";
            }
        }
    }
}